package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

import com.model.Member;
import com.model.Member;
import com.service.AgentService;
import com.service.BranchService;
import com.service.GroupService;
import com.service.MemberService;
import com.service.MemberService;

@Controller
public class MemberController {

	@Autowired
	private MemberService memberService;
	@Autowired
	private BranchService branchService;
	@Autowired
	private GroupService groupService;
	@Autowired
	private AgentService agentService;
		
	// REquest to add member.. Give blank form of member
	@RequestMapping(value="/user/addMember",method=RequestMethod.GET)
	public String addMember(ModelMap model,@ModelAttribute("command") Member member,HttpSession session) {
		model.put("groups", groupService.getGroupName());
		model.put("agents", agentService.getAgentName());
		return "member";
	}
	
	// add member details into database
	@RequestMapping(value = "/user/saveMember", method = RequestMethod.POST)
	public String saveMember(HttpSession session,@ModelAttribute("command") Member member) {
			memberService.add(session.getAttribute("branch").toString(),member);
			return "redirect:/user/addMember.htm";
	}
	
	// give member details from database
	@RequestMapping("/user/editMember")
    public ModelAndView editMember(@ModelAttribute("command") Member member,
    		HttpServletRequest request)
    {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("member",memberService.get(id));
        return new ModelAndView("member",model);
    }
	
	//delete Member
	@RequestMapping("/admin/deleteMember")
    public String deleteMember(HttpServletRequest request)
    {
		Integer id=Integer.parseInt(request.getParameter("id"));
        memberService.delete(id);
        return "redirect:/admin/addMember.htm";
    }
	
	
}
